package com.cloudfun.campusshare.entity;

import com.cloudfun.campusshare.common.constant.LoanStatus;
import lombok.*;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.List;

/**
 * Created by Huqin on 2020/3/4
 */
@Data
@Builder
@Entity
@ToString(exclude = {"borrower", "projects"})
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "loan")
public class LoanEntity extends BaseEntity {

    @Id
    @GenericGenerator(name = "id_uuid", strategy = "uuid")
    @GeneratedValue(generator = "id_uuid")
    @Column(name = "id", unique = true, columnDefinition = ("varchar(40) not null comment '主键uuid'"))
    private String id;

    @Column(columnDefinition = ("varchar(31) default null comment '编号'"))
    private String loanNo;

    @ManyToOne
    @JoinColumn(name = "borrower_id", insertable = false, updatable = false)
    private UserEntity borrower;

    @Column(name = "borrower_id", columnDefinition = ("varchar(40) default null comment '借款人/运营商Id'"))
    private String borrowerId;

    @ManyToOne
    @JoinColumn(name = "lender_id", insertable = false, updatable = false)
    private UserEntity lender;

    @Column(name = "lender_id", columnDefinition = ("varchar(40) default null comment '金融机构/贷款人id'"))
    private String lenderId;

    @Column(columnDefinition = ("decimal(18,2) default null comment '贷款金额'"))
    private Double loanAmount;

    @Fetch(FetchMode.SUBSELECT)
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "loan")
    private List<ProjectEntity> projects;

    @Column(columnDefinition = ("decimal(18,2) default null comment '实际放款金额'"))
    private Double actualAmount;

    @Column(columnDefinition = ("varchar(512) default null comment '用途'"))
    private String purpose;

    @Column(columnDefinition = ("varchar(32) not null comment '状态'"))
    @Enumerated(EnumType.STRING)
    private LoanStatus status;

    @Column(columnDefinition = ("decimal(18,2) default null comment '放款金额'"))
    private Double issueAmount;

    @Column(columnDefinition = "varchar(14) default null comment '放款时间'")
    private String issueTime;

    @Column(columnDefinition = "varchar(14) default null comment '放款确认时间'")
    private String confirmTime;
}
